/**
 * 
 */
package com.jdxx.service.impl;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;
import org.eredlab.g4.bmf.base.BaseServiceImpl;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;

import com.jdxx.service.ShebeidaService;
import com.jdxx.util.JdxxConstants;

/**
 * 
 * @author sukairong
 */
public class ShebeidaServiceImpl extends BaseServiceImpl implements
		ShebeidaService {

	/* (non-Javadoc)
	 * @see com.jdxx.service.ShebeidaService#saveShebeida(org.eredlab.g4.ccl.datastructure.Dto)
	 */
	public Dto saveShebeida(Dto pDto) {
		Dto outDto = new BaseDto();
		//获取档案编号
		Dto pDtodeptbyid = new BaseDto();
		pDtodeptbyid.put("deptid", pDto.get("bumenbh"));
		outDto.putAll((BaseDto) g4Dao.queryForObject("Organization.queryDeptinfoByDeptid", pDtodeptbyid));
		String newshebeibh = getShebeibh("YH",outDto.getAsString("customid") , pDto.getAsString("xitonglb"), pDto.getAsString("shebeilx"));
		pDto.put("chuchangrq", pDto.getAsDate("chuchangrq"));
		pDto.put("diaorurq", pDto.getAsDate("diaorurq"));
		pDto.put("shebeibh", newshebeibh);
		pDto.put("chuangjiaren", pDto.getAsString("loginuserid"));
		pDto.put("chuangjiansj",Calendar.getInstance().getTime());
		pDto.put("zuofeibz", JdxxConstants.ZFBZ_YX);

		g4Dao.insert("Shebeida.saveShebeida", pDto);
		outDto.put("msg", "设备档案新建成功");
		outDto.put("success", new Boolean(true));
		return outDto;
	}
	
	/**
	 * 获取设备编号
	 * @param gsbm 公司编号
	 * @param bmzdybh 部门自定义编号
	 * @param xtlb 系统类别
	 * @param sblx 设备类型
	 * @return
	 */
	private String getShebeibh(String gsbm,String bmzdybh,String xtlb,String sblx){
		String paraStr = gsbm.concat(bmzdybh).concat(xtlb).concat(sblx);
		Dto pDto = new BaseDto();
		pDto.put("queryParam", paraStr);
		Object obj = g4Dao.queryForObject("Shebeida.queryMaxShebeibh",pDto);
		Integer maxnum = 0;
		if(obj != null){
			BaseDto dto = (BaseDto)obj;
			String maxShebeibh = dto.getAsString("shebeibh");
			maxnum = new Integer(maxShebeibh.substring(maxShebeibh.length()-3, maxShebeibh.length()));
		}
		Integer maxnumaddone = maxnum + 1;
		String maxnumaddoneString = maxnumaddone.toString();
		if(maxnumaddoneString.length() == 1){
			maxnumaddoneString = "00"+maxnumaddoneString;
		}else if(maxnumaddoneString.length() == 2){
			maxnumaddoneString = "0"+maxnumaddoneString;
		}
		return paraStr+maxnumaddoneString;
	}

}
